package com.example.charges.activities.login;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.charges.ControllerCharge;
import com.example.charges.dati.domain.Utenti;
import com.example.charges.dati.domain.dao.WrapUtenti;
import com.molinari.androidstructure.activity.BaseActivity;
import com.molinari.androidstructure.listener.OnClickApriActivity;

public class OnClickBtnSignListener extends OnClickApriActivity {

	public OnClickBtnSignListener(Class<?> classe, BaseActivity activity) {
		super(classe, activity);
	}

	@Override
	protected boolean doSomethingPreStart(View v) {
		super.doSomethingPreStart(v);
		LoginActivity activity = ((LoginActivity)getActivity());
		
		final WrapUtenti wrap = new WrapUtenti(activity);
		final EditText user = activity.getUser();
		final EditText pass = activity.getPass();
		
		String username = user.getText() != null ? user.getText().toString() : "";
		String password = pass.getText() != null ? pass.getText().toString() : "";
		
		final Utenti utente = wrap.selectByUserAndPass(username, password);
		if (utente != null) {
			ControllerCharge.getSingleton().setUtenteLogin(utente);
			return true;
		}else{
			Toast.makeText(activity.getApplication(), "Utente non esistente: correggi o registra", Toast.LENGTH_SHORT).show();
			return false;
		}
	}
}
